package com.jvyou.app.ds.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.jvyou.app.common.domain.dto.DictDataDto;
import com.jvyou.app.common.domain.entity.DictData;
import com.jvyou.app.common.domain.model.PageResult;
import com.jvyou.app.common.domain.model.Result;
import com.jvyou.app.common.enums.HttpCodeEnum;
import com.jvyou.app.ds.service.DictDataService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 字典数据表(DictData)表控制层
 *
 * @author 橘柚
 * @since 2025-03-13 09:50:54
 */
@RestController
@RequestMapping("/dictData")
public class DictDataController {

    @Resource
    private DictDataService dictDataService;

    /**
     * 查询全部数据
     *
     * @return 列表
     */
    @GetMapping("/list")
    @SaCheckPermission("system:dictData:list")
    public Result<List<DictData>> list(@ModelAttribute DictDataDto dictDataDto) {
        return Result.success(dictDataService.listDictData(dictDataDto));
    }

    /**
     * 分页查询数据
     *
     * @return 列表
     */
    @GetMapping("/page")
    @SaCheckPermission("system:dictData:page")
    public Result<PageResult<DictData>> page(@ModelAttribute DictDataDto dictDataDto) {
        return Result.success(dictDataService.pageDictData(dictDataDto));
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    @SaCheckPermission("system:dictData:get")
    public Result<DictData> selectOne(@PathVariable Long id) {
        return Result.success(dictDataService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param dictData 实体对象
     * @return 新增结果
     */
    @PostMapping
    @SaCheckPermission("system:dictData:insert")
    public Result<DictData> insert(@RequestBody DictData dictData) {
        return dictDataService.insertDictData(dictData);
    }

    /**
     * 修改数据
     *
     * @param dictData 实体对象
     * @return 修改结果
     */
    @PutMapping("/updateById")
    @CacheEvict(cacheNames = "dictData", key = "#dictTypeId + ':' + #dictValue")
    @SaCheckPermission("system:dictData:update")
    public Result<DictData> updateById(@RequestBody DictData dictData) {
        boolean success = dictDataService.updateById(dictData);
        return success ? Result.success(dictData) : Result.error(HttpCodeEnum.UPDATE_FAIL);
    }


    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @SaCheckPermission("system:dictData:delete")
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean success = dictDataService.removeById(id);
        return success ? Result.success(true) : Result.error(HttpCodeEnum.DELETE_FAIL);
    }

}

